home *** CD-ROM | disk | FTP | other *** search
Text File | 1997-06-13 | 8.1 KB | 329 lines | [TEXT/CWIE] |
-
- //===============================================================================
- //
- // ScrapPickerDraw.c
- //
- // Copyright: © 1997 by Apple Computer, Inc., all rights reserved.
- // by john calhoun & David Hayward
- //
- //===============================================================================
-
-
- #include <Folders.h> // <-- Public interfaces.
- #include <Resources.h>
- #include <TextUtils.h>
- #include "ScrapPicker.h" // <-- Project interfaces.
- #include "PickerCommon.h"
-
-
- static void DrawColorList (PickerStorageHandle);
- static void DrawColorRects (PickerStorageHandle storage, Boolean drawOrig);
- static void DrawChangedProc (PickerStorageHandle storage);
- static pascal void DrawNewColorProc (SInt16, SInt16, GDHandle, SInt32);
-
-
- #if GENERATINGCFM
- RoutineDescriptor DrawNewColorProcRD =
- BUILD_ROUTINE_DESCRIPTOR (uppDeviceLoopDrawingProcInfo, DrawNewColorProc);
- #endif
-
-
- //===================================================================== Functions
- //--------------------------------------------------------------------- DrawColorEditor
- // Draws all sliders and updates the edit text items.
-
- void DrawColorEditor (PickerStorageHandle storage, UInt32 flags)
- {
- PickerStoragePtr pStorage;
-
- pStorage = *storage;
-
- if (!pStorage->visible)
- return;
-
- if ((flags & kDrawOrig) && (flags & kDrawNew))
- DrawColorList(storage);
- else
- {
- if (flags & kDrawOrig)
- DrawColorRects(storage,true);
- if (flags & kDrawNew)
- DrawColorRects(storage,false);
- }
-
- if (flags & kDrawPict)
- {
- PicHandle thePict;
- OSErr theErr;
- SInt16 resFile;
-
- // -1 and 0 for pictNumber means to use one of the two internal…
- // PICTs in this picker. Positive number mean grab it from the…
- // scrapbook.
- if (pStorage->pictNumber <= 0)
- {
- // Open our resource fork.
- resFile = OpenComponentResFile((Component)pStorage->myself);
- require_action(resFile > 0, fail, theErr = cantLoadPicker;);
- }
- else
- {
- resFile = OpenScrapBookFile();
- require_action(resFile > 0, fail, theErr = ResError(););
- }
-
- if (pStorage->pictNumber < 0)
- thePict = GetPicture(128);
- else if (pStorage->pictNumber == 0)
- thePict = GetPicture(129);
- else
- thePict = (PicHandle)Get1IndResource('PICT', pStorage->pictNumber);
-
- if (thePict)
- {
- Rect bounds, pictBounds;
-
- GetItemRect(storage, iPicture, &bounds);
- EraseRect(&bounds);
-
- HLock((Handle)thePict);
- pictBounds = (*thePict)->picFrame;
- HUnlock((Handle)thePict);
-
- // Is picture smaller than item rect?
- if ((pictBounds.right - pictBounds.left) < (bounds.right - bounds.left))
- {
- SInt16 originalWidth;
-
- originalWidth = bounds.right - bounds.left;
- bounds.left += (originalWidth - (pictBounds.right - pictBounds.left)) / 2;
- bounds.right = bounds.left + (pictBounds.right - pictBounds.left);
- }
-
- if ((pictBounds.bottom - pictBounds.top) < (bounds.bottom - bounds.top))
- {
- SInt16 originalHeight;
-
- originalHeight = bounds.bottom - bounds.top;
- bounds.top += (originalHeight - (pictBounds.bottom - pictBounds.top)) / 2;
- bounds.bottom = bounds.top + (pictBounds.bottom - pictBounds.top);
- }
-
- DrawPicture(thePict, &bounds);
- ReleaseResource((Handle)thePict);
- }
-
- if (pStorage->pictNumber <= 0)
- {
- theErr = CloseComponentResFile(resFile);
- check(theErr == noErr);
- }
- else
- CloseResFile(resFile);
- }
-
- fail:
-
- if (flags & kDrawProc)
- DrawChangedProc(storage);
- }
-
- //--------------------------------------------------------------------- OpenScrapBookFile
-
- SInt16 OpenScrapBookFile (void)
- {
- CInfoPBRec theBlock;
- CInfoPBRec *theBlockPtr;
- Str255 fileName;
- SInt32 foundDirID;
- OSErr theErr;
- SInt16 foundVRefNum;
- SInt16 resFile, count;
-
- resFile = -1;
-
- theErr = FindFolder(kOnSystemDisk, kSystemFolderType, kDontCreateFolder,
- &foundVRefNum, &foundDirID);
- require(theErr == noErr, fail);
-
- count = 1;
- theBlockPtr = &theBlock;
- theBlockPtr->dirInfo.ioCompletion = 0L;
- theBlockPtr->dirInfo.ioVRefNum = foundVRefNum;
- theBlockPtr->dirInfo.ioFVersNum = 0;
- theBlockPtr->dirInfo.ioFDirIndex = count;
- theBlockPtr->dirInfo.ioNamePtr = fileName;
- theBlockPtr->dirInfo.ioDrDirID = foundDirID;
-
- do
- {
- theErr = PBGetCatInfoSync(theBlockPtr);
- if (theErr == noErr)
- {
- if ((theBlockPtr->hFileInfo.ioFlFndrInfo.fdType == 'sbkt') ||
- (theBlockPtr->hFileInfo.ioFlFndrInfo.fdType == 'sbkt'))
- goto done;
- }
-
- nextFile:
-
- count++;
- theBlockPtr->hFileInfo.ioFDirIndex = count;
- theBlockPtr->hFileInfo.ioDirID = foundDirID;
- }
- while (theErr == noErr);
-
- if (theErr == fnfErr)
- theErr = noErr;
- else
- goto fail;
-
- done:
-
- resFile = HOpenResFile(foundVRefNum, foundDirID, fileName, fsCurPerm);
-
- fail:
-
- return resFile;
- }
-
- #pragma mark -------------------- Private Functions
- //===================================================================== Private Functions
- //--------------------------------------------------------------------- DrawColorList
- // Draws the two (original and new) color rectangle frames in the top
- // right of the picker. Also, calls the function to fill them with color.
-
- static void DrawColorList (PickerStorageHandle storage)
- {
- Rect oneRect;
- PickerStoragePtr pStorage;
- PenState oldPen;
-
- pStorage = *storage;
-
- if (!pStorage->visible)
- return;
-
- GetPenState(&oldPen);
- PenNormal();
- ForeColor(blackColor);
-
- GetItemRect(storage, iOrigColor, &oneRect);
- DropRectShadow(&oneRect, 5, (pStorage->depth > 4));
- GetItemRect(storage, iNewColor, &oneRect);
- DropRectShadow(&oneRect, 5, (pStorage->depth > 4));
-
- DrawColorRects(storage, true);
- DrawColorRects(storage, false);
-
- ForeColor(blackColor);
- SetPenState(&oldPen);
- }
-
- //--------------------------------------------------------------------- DrawColorRects
- // Fills in the color rectangles (original and new) in the top right
- // corner of the picker. Will handle the nasty B&W case as well.
-
- static void DrawColorRects (PickerStorageHandle storage, Boolean drawOrig)
- {
- Rect theRect;
- RgnHandle newColorRgn;
- PixPatHandle hPattern;
- PickerStoragePtr pStorage;
- DeviceLoopDrawingUPP drawNewColorProcUPP;
-
- pStorage = *storage;
-
- #if GENERATINGCFM
- drawNewColorProcUPP = &DrawNewColorProcRD;
- #else
- drawNewColorProcUPP = &DrawNewColorProc;
- #endif
-
- PenNormal();
-
- if (drawOrig)
- {
- GetItemRect(storage, iOrigColor, &theRect);
- InsetRect(&theRect, 1, 1);
- hPattern = pStorage->origColorPat;
-
- if (pStorage->useColorPats)
- {
- // Draw the old color rect.
- FillCRect(&theRect, hPattern);
- check(QDError() == noErr);
- }
- else
- {
- // Draw B&W old color
- HLock((Handle)hPattern);
- FillRect(&theRect, (ConstPatternParam)*hPattern);
- HUnlock((Handle)hPattern);
- }
- }
- else
- {
- GetItemRect(storage, iNewColor, &theRect);
- InsetRect(&theRect, 1, 1);
- hPattern = pStorage->newColorPat;
-
- SetColorPattern(storage, kNewColor);
- if (pStorage->useColorPats)
- {
- // Draw the new color rect.
- newColorRgn = NewRgn();
- if (newColorRgn)
- {
- RectRgn(newColorRgn, &theRect);
- ClipRect(&theRect);
- DeviceLoop(newColorRgn, drawNewColorProcUPP, (long)storage, 0);
- DisposeRgn(newColorRgn);
- ClipRect(&pStorage->port->portRect);
- }
- }
- else
- {
- // Draw B&W new color
- HLock((Handle)hPattern);
- FillRect(&theRect, (ConstPatternParam)*hPattern);
- HUnlock((Handle)hPattern);
- }
- }
- }
-
- //--------------------------------------------------------------------- DrawChangedProc
-
- static void DrawChangedProc (PickerStorageHandle storage)
- {
- PickerStoragePtr pStorage;
-
- pStorage = *storage;
-
- // Call the app's color-changed procedure (if any).
- if ((!pStorage->calledNPickColor) && pStorage->colorProc)
- {
- PMColor color;
-
- (void) doPickerGetColor(storage, kNewColor, &color);
- CallColorChangedProc((*storage)->colorProc, (*storage)->colorProcData, &color);
- }
- }
-
- //--------------------------------------------------------------------- DrawNewColorProc
-
- static pascal void DrawNewColorProc (short depth, short deviceFlags, GDHandle targetDevice,
- SInt32 storage)
- {
- #pragma unused (deviceFlags, depth)
- GDHandle saveGD;
-
- saveGD = GetGDevice();
- SetGDevice(targetDevice);
-
- FillCRect(&(((*(PickerStorageHandle)storage)->port)->portRect), (*(PickerStorageHandle)storage)->newColorPat);
-
- SetGDevice(saveGD);
- }
-
-